﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace VueShopApi.Common
{
    public class ServerResponse
    {
        #region Json Result

        private static IActionResult BuildResult(int status, string msg, object data)
        {
            return new JsonResult(new { meta = new { status, msg }, data });
        }

        #region Success Reuslt 

        public static IActionResult Yes(object data, string msg = "success")
        {
            return BuildResult(200, msg, data);
        }

        public static IActionResult Yes(string msg = "success")
        {
            return BuildResult(200, msg, null);
        }

        public static IActionResult Created(string msg = "Created")
        {
            return BuildResult(201, msg, null);
        }

        public static IActionResult NoContent(string msg = "No Content")
        {
            return BuildResult(204, msg, null);
        }

        #endregion

        #region   Failure Result

        public static IActionResult BadRequest(string msg = "BadRequest")
        {
            return BuildResult(400, msg, null);
        }

        public static IActionResult Unauthorized(string msg = "Unauthorized")
        {
            return BuildResult(401, msg, null);
        }

        public static IActionResult Forbidden(string msg = "Forbidden")
        {
            return BuildResult(403, msg, null);
        }

        public static IActionResult NotFound(string msg = "Not Found")
        {
            return BuildResult(404, msg, null);
        }

        public static IActionResult NotAllowed(string msg = "Method not allowed")
        {
            return BuildResult(405, msg, null);
        }


        public static IActionResult No(object data, string msg = "failure")
        {
            return BuildResult(500, msg, data);
        }

        public static IActionResult No(string msg = "failure")
        {
            return BuildResult(500, msg, null);
        }

        public static IActionResult BadGateway(string msg = "Bad Gateway")
        {
            return BuildResult(502, msg, null);
        }

        public static IActionResult ServiceUnavailable(string msg = "Service Unavailable")
        {
            return BuildResult(503, msg, null);
        }

        #endregion

        #endregion

        #region Text Result

        private static string BuildTextResult(int status, string msg, object data)
        {
            return JsonConvert.SerializeObject(new { meta = new { status, msg }, data });
        }

        public static string YesText(string msg, object data = null)
        {
            return BuildTextResult(200, msg, data);
        }

        public static string UnauthorizedText(string msg, object data = null)
        {
            return BuildTextResult(401, msg, data);
        }

        public static string NoText(string msg, object data = null)
        {
            return BuildTextResult(500, msg, data);
        }

        #endregion
    }
}
